package ogmios.dialogs;

import ogmios.components.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 * A dialog that allows users to save multiple Savable items at once.
 */
public class MultiSaveDialog extends JDialog
{
 private final JList saveList;
 private final DefaultListModel saveListModel;
 public MultiSaveDialog()
 {
  setModal(true);
  setTitle("Save"); 
  setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  Container contents = getContentPane();
  contents.add(new JScrollPane(saveList = new JList(saveListModel = new DefaultListModel())));
  JPanel eastPanel = new JPanel();
  eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
  JButton saveButton = new JButton("Save");
  JButton saveAllButton = new JButton("Save All");
  JButton discardButton = new JButton("Discard All");
  JButton cancelButton = new JButton("Cancel");
  eastPanel.add(saveButton);
  eastPanel.add(saveAllButton);
  eastPanel.add(discardButton);
  eastPanel.add(cancelButton);
  contents.add(eastPanel, BorderLayout.EAST);
  saveButton.addActionListener(
     new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         int[] indices = saveList.getSelectedIndices();
         if (indices.length != 0)
         {
           for (int i = indices.length - 1; i != -1; i--)
          {
             int index;
             Savable savable = (Savable)saveListModel.getElementAt(index=indices[i]);
             savable.save();
             if (!savable.saveNeeded())
              saveListModel.remove(index);
           }
           if (saveListModel.size() == 0)
               MultiSaveDialog.this.setVisible(false);
           }

          }
         }
       );
  saveAllButton.addActionListener(
   new ActionListener()
   {
     public void actionPerformed(ActionEvent e)
     {
       for (int i = saveListModel.size() - 1; i != -1; i--)
       {
         Savable savable = (Savable)saveListModel.getElementAt(i);
         savable.save();
         if (!savable.saveNeeded())
          saveListModel.remove(i);
       }
       MultiSaveDialog.this.setVisible(false);
      }
     }
   );
  discardButton.addActionListener(
   new ActionListener()
   {
     public void actionPerformed(ActionEvent e)
    {
      (saveListModel).removeAllElements();
      MultiSaveDialog.this.setVisible(false);
    }
  }
  );
  cancelButton.addActionListener(
  new ActionListener()
   {
     public void actionPerformed(ActionEvent e)
     {
       MultiSaveDialog.this.setVisible(false);
      }
    }
  );
  pack();
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  setSize(new Dimension(screenSize.width / 4, screenSize.height / 4));
 }

 public void addSavable(Savable savable)
 {
  if (savable.saveNeeded())
   saveListModel.addElement(savable);
 }
 
 public void removeSavable(Savable savable)
 {
  saveListModel.removeElement(savable);
 }

 /**
 * Will display this SaveDialog if there is at least one Savable currently in the queue.
 * <br>
 * Returns true if all Savable items within the SaveDialog have either been
 * discarded or saved. 
 */
 public boolean canClose()
 {
  return canClose(null);
 }
 /**
 * Will display this SaveDialog if there is at least one Savable currently in the queue.
 * <br>
 * Returns true if all Savable items within the SaveDialog have either been
 * discarded or saved.
 *
 * @param     component     The component to set this SaveDialog relative to.
 */
 public boolean canClose(Component component)
 {
   setLocationRelativeTo(component);
   if (saveListModel.size() > 0)
   {
    setVisible(true);
    if (saveListModel.size() > 0)
    return false;
   }
   return true;
 }

}
